package io.renren.modules.police.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.police.dao.PoliceReportDao;
import io.renren.modules.police.dto.PoliceCaseDTO;
import io.renren.modules.police.dto.PoliceReportDTO;
import io.renren.modules.police.entity.PoliceReportEntity;
import io.renren.modules.police.service.PoliceCaseService;
import io.renren.modules.police.service.PoliceReportService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 报案信息表（立案后 -> 案件信息表）
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-09-02
 */
@Service
public class PoliceReportServiceImpl extends CrudServiceImpl<PoliceReportDao, PoliceReportEntity, PoliceReportDTO> implements PoliceReportService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private PoliceCaseService policeCaseService;

    @Override
    public QueryWrapper<PoliceReportEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String keyWord = (String)params.get("keyWord");
        String reportType = (String)params.get("reportType");

        QueryWrapper<PoliceReportEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        if (StringUtils.isNotBlank(reportType)) {
            wrapper.eq("report_type", reportType);
        }
        if (StringUtils.isNotBlank(keyWord)) {
            List<SysUserDTO> sysUserDTOList = sysUserService.getByRealName(keyWord);
            if (CollectionUtils.isEmpty(sysUserDTOList)) {
                wrapper.like("title", keyWord);
            } else {
                List<Long> collect = sysUserDTOList.stream().map(e -> e.getId()).collect(Collectors.toList());
                wrapper.and(wp -> wp.like("title", keyWord).or().in("creator", collect));
            }
        }

        return wrapper;
    }


    @Override
    public void saveReport(PoliceReportDTO dto) {
        dto.setStatus(0);
        dto.setRegisterStatus(0);
        if (CollectionUtils.isNotEmpty(dto.getImgUrlLists())) {
            dto.setImgUrlList(StringUtils.join(dto.getImgUrlLists(), ","));
        }
        this.save(dto);
    }

    @Override
    public List<PoliceReportDTO> getReportByRegisterStatus(Integer registerStatus) {
        Long userId = SecurityUser.getUserId();
        QueryWrapper<PoliceReportEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("register_status", registerStatus);
        wrapper.eq("creator", userId);
        List<PoliceReportEntity> entityList = this.baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(entityList, PoliceReportDTO.class);
    }

    @Override
    public PageData<PoliceReportDTO> getPage(Map<String, Object> params) {
        PageData<PoliceReportDTO> page = this.page(params);
        page.getList().forEach(e -> {
            if (StringUtils.isNotBlank(e.getImgUrlList())) {
                e.setImgUrlLists(Arrays.asList(e.getImgUrlList().split(",")));
            }
            SysUserDTO creatorDTO = sysUserService.get(e.getCreator());
            e.setCreatorName(creatorDTO.getRealName());
            SysUserDTO updaterDTO = sysUserService.get(e.getUpdater());
            e.setUpdaterName(updaterDTO.getRealName());
        });
        return page;
    }

    @Override
    public void updateReport(PoliceReportDTO dto) {
        this.update(dto);
        if (dto.getRegisterStatus() != null && dto.getRegisterStatus() == 1) {
            PoliceReportEntity policeReportEntity = this.baseDao.selectById(dto.getId());
            PoliceCaseDTO policeCaseDTO = new PoliceCaseDTO();
            policeCaseDTO.setTitle(policeReportEntity.getTitle());
            policeCaseDTO.setContent(policeReportEntity.getContent());
            policeCaseDTO.setImgUrlList(policeReportEntity.getImgUrlList());
            policeCaseDTO.setReportType(policeReportEntity.getReportType());
            policeCaseDTO.setReportUserId(policeReportEntity.getCreator());
            policeCaseDTO.setStatus(0);
            policeCaseService.save(policeCaseDTO);
        }
    }

    @Override
    public PoliceReportDTO getReportDetail(Long id) {
        PoliceReportDTO policeReportDTO = this.get(id);
        if (StringUtils.isNotBlank(policeReportDTO.getImgUrlList())) {
            policeReportDTO.setImgUrlLists(Arrays.asList(policeReportDTO.getImgUrlList().split(",")));
        }
        SysUserDTO creatorDTO = sysUserService.get(policeReportDTO.getCreator());
        policeReportDTO.setCreatorName(creatorDTO.getRealName());
        SysUserDTO updaterDTO = sysUserService.get(policeReportDTO.getUpdater());
        policeReportDTO.setUpdaterName(updaterDTO.getRealName());
        return policeReportDTO;
    }
}